﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Greve
{
    public partial class Resultat : Form
    {
        public delegate void TickHandler(Resultat r, EventArgs e);

        public event TickHandler Tick;

        public EventArgs ee = null;

		public Dictionary<String, List<PersonResult>> resultList;
		//public List<PersonResultLabels> resultLabels;

        private String selectedCup;
        private int selectedDiver;
        private int selectedDive;
        public Resultat()
        {
			InitializeComponent();
			resultList = new Dictionary<String,List<PersonResult>>();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
			
        }

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
            selectedCup = comboBoxCupList.SelectedItem.ToString();
            comboBoxJumper.Items.Clear();
            for(int i =0; i < resultList[selectedCup].Count; ++i)
            {
                comboBoxJumper.Items.Add(resultList[selectedCup][i].diverName);
            }
		}

		public void addPerson(String p)
		{
            Invoke((MethodInvoker)delegate
            {
                addJumper(p);
            });
		}
        private void addJumper(String p)
        {
            int index = 0;
            bool exists = false;
            String cupname = p.Split('|')[0];
            String per = p.Split('|')[1];
            String[] perSplit = per.Split(' ');

            if (!comboBoxCupList.Items.Contains(cupname))
                comboBoxCupList.Items.Add(cupname);

            if (!resultList.ContainsKey(cupname))
                resultList[cupname] = new List<PersonResult>();
            if(resultList[cupname].Count != 0)
                foreach (PersonResult QQ in resultList[cupname])
                {
                    if (QQ.diverName.Equals(perSplit[0].ToString()))
                    {
                        exists = true;
                        break;
                    }
                    else
                        ++index;
                }
            if (!exists)
            {
                PersonResult res = new PersonResult();
                res.diverName = perSplit[0].ToString();
                res.totalScore = double.Parse(perSplit[2].ToString());
                res.dive = new List<Tuple<string, double>>();
                foreach (String v in perSplit)
                {
                    if (v.Contains("Dive"))
                    {
                        String[] q = v.Split('<');
                        res.dive.Add(new Tuple<String, double>(q[4], double.Parse(q[5])));
                    }
                }
                resultList[cupname].Add(res);
            }
            else
            {
                resultList[cupname][index].totalScore = double.Parse(perSplit[2].ToString());
                resultList[cupname][index].dive.Clear();
                foreach (String v in perSplit)
                {
                    if (v.Contains("Dive"))
                    {
                        String[] q = v.Split('<');
                        resultList[cupname][index].dive.Add(new Tuple<String, double>(q[4], double.Parse(q[5])));
                    }
                }
            }
        }
		public static void updateList()
		{

		}
		
		public void updatePerson()
		{
			for(int i = 0; i < resultList.Count(); i++)
			{
				
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
            if (Tick != null)
            {
                Tick(this, ee);
            }
		}

        private void comboBoxJumper_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedDiver = comboBoxJumper.SelectedIndex;
            comboBoxDive.Items.Clear();
            label6.Text = resultList[selectedCup][selectedDiver].totalScore.ToString();
            for(int i =0; i < resultList[selectedCup][selectedDiver].dive.Count; ++i)
            {
                comboBoxDive.Items.Add(resultList[selectedCup][selectedDiver].dive[i].Item1.ToString());
            }
        }

        private void comboBoxDive_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedDive = comboBoxDive.SelectedIndex;
            label4.Text = resultList[selectedCup][selectedDiver].dive[selectedDive].Item2.ToString();
        }
    }
}
